package com.ruoyi.web.controller.system;

import java.util.List;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysSort;
import com.ruoyi.system.service.ISysSortService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分类管理Controller
 *
 * @author ruoyi
 * @date 2022-02-15
 */
@RestController
@RequestMapping("/system/sort")
public class SysSortController extends BaseController
{
    @Autowired
    private ISysSortService sysSortService;

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:sort:list')")
    @ApiOperation("查询分类管理列表")
    @GetMapping("/list")
    public TableDataInfo list(SysSort sysSort)
    {
        startPage();
        List<SysSort> list = sysSortService.selectSysSortList(sysSort);
        return getDataTable(list);
    }

    /**
     * 导出分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:sort:export')")
    @Log(title = "分类管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysSort sysSort)
    {
        List<SysSort> list = sysSortService.selectSysSortList(sysSort);
        ExcelUtil<SysSort> util = new ExcelUtil<SysSort>(SysSort.class);
        return util.exportExcel(list, "sort");
    }

    /**
     * 获取分类管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sort:query')")
    @ApiOperation("获取分类管理详细信息")
    @ApiImplicitParam(name = "recNo", value = "分类管理编号", required = true, dataType = "String", paramType = "传分类管理编号")
    @GetMapping(value = "/{recNo}")
    public AjaxResult getInfo(@PathVariable("recNo") String recNo)
    {
        return AjaxResult.success(sysSortService.selectSysSortById(recNo));
    }

    /**
     * 新增分类管理
     */
    @PreAuthorize("@ss.hasPermi('system:sort:add')")
    @Log(title = "分类管理", businessType = BusinessType.INSERT)
    @ApiOperation("新增分类管理")
    @ApiImplicitParam(name = "sysSort", value = "分类管理对象", required = true, dataType = "SysSort", paramType = "传分类管理对象")
    @PostMapping
    public AjaxResult add(@RequestBody SysSort sysSort)
    {
        return toAjax(sysSortService.insertSysSort(sysSort));
    }

    /**
     * 修改分类管理
     */
    @PreAuthorize("@ss.hasPermi('system:sort:edit')")
    @Log(title = "分类管理", businessType = BusinessType.UPDATE)
    @ApiOperation("修改分类管理")
    @ApiImplicitParam(name = "sysSort", value = "分类管理对象", required = true, dataType = "SysSort", paramType = "传分类管理对象")
    @PostMapping("/editList")
    public AjaxResult edit(@RequestBody SysSort sysSort)
    {
        return toAjax(sysSortService.updateSysSort(sysSort));
    }

    /**
     * 删除分类管理
     */
    @PreAuthorize("@ss.hasPermi('system:sort:remove')")
    @Log(title = "分类管理", businessType = BusinessType.DELETE)
    @ApiOperation("删除分类管理")
    @ApiImplicitParam(name = "recNos", value = "分类管理编号数组", required = true, dataType = "String[]", paramType = "传分类管理编号数组")
    @GetMapping("/delete/{recNos}")
    public AjaxResult remove(@PathVariable String[] recNos)
    {
        if (sysSortService.deleteSysSortByIds(recNos)==-1){
            return AjaxResult.error("该分类还有子分类不能删除");
        }
        return toAjax(sysSortService.deleteSysSortByIds(recNos));
    }

    /**
     * 查询分类管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:sort:list')")
    @ApiOperation("查询分类管理列表")
    @ApiImplicitParam(name = "sysSort", value = "分类管理对象", required = true, dataType = "SysSort", paramType = "传分类管理对象")
    @GetMapping("/lists")
    public TableDataInfo lists(SysSort sysSort)
    {
        List<SysSort> list = sysSortService.selectSysSortLists(sysSort);
        return getDataTable(list);
    }

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('system:sort:list')")
    @ApiOperation("查询列表")
    @ApiImplicitParam(name = "sysSort", value = "分类管理对象", required = true, dataType = "SysSort", paramType = "传分类管理对象")
    @GetMapping("/querylist")
    public TableDataInfo querylList(SysSort sysSort)
    {
        startPage();
        List list = sysSortService.querylList(sysSort);
        return getDataTable(list);
    }
}
